package com.sinog.biz.service.business.impl;

import com.sinog.biz.mapper.havexml.OptFzlxMapper;
import com.sinog.biz.mapper.havexml.PrisonerFxMapper;
import com.sinog.biz.service.business.OptFzlxService;
import com.sinog.core.model.FzlxIdVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @desc 更新库中的犯罪类型
 * @author yanhao
 * @date 2021/4/8 14:23
 **/
@Slf4j
@Service
public class OptFzlxServiceImpl implements OptFzlxService {

    @Resource
    private OptFzlxMapper optFzlxMapper;
    @Resource
    private PrisonerFxMapper prisonerFxMapper;

    /**
     * 描述
     * @return String
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String updateFzlx() {
        List<FzlxIdVo> fzlxList = prisonerFxMapper.findFzlx();
        List<FzlxIdVo> collect = fzlxList.stream().peek(item -> {
            String oldFzlx = item.getFzlx();
            Set<String> set = new HashSet<>(16);
            String[] split = StringUtils.split(oldFzlx,",");
            for(String s : split) {
                String id = optFzlxMapper.getFzlxById(s);
                set.add(id);
            }
            item.setFzlx(String.join(",",set));
        }).collect(Collectors.toList());
        if(!collect.isEmpty()) {
            collect.forEach(item -> prisonerFxMapper.updateFzlx1(item));
            return "ok";
        }
        return null;
    }
}